<template>
  <div>
    <p v-if="!menus.length">正在加载菜单数据...</p>
    <el-menu v-else :default-active="activeMenu" mode="vertical">
      <template v-for="menu in menus" :key="menu.id">
        <el-sub-menu v-if="menu.children && menu.children.length > 0" :index="menu.id.toString()">
          <template #title>
            <i class="el-icon-location"></i>
            <span>{{ menu.name }}</span>
          </template>
          <el-menu-item v-for="child in menu.children" :key="child.id" :index="child.id.toString()">
            {{ child.name }}
          </el-menu-item>
        </el-sub-menu>
        <el-menu-item v-else :index="menu.id.toString()">
          {{ menu.name }}
        </el-menu-item>
      </template>
    </el-menu>
  </div>
</template>

<script lang="ts" setup>
import { useUserStore } from "@/store/user";
import { onMounted, toRefs } from "vue";

const userStore = useUserStore();
const { menus } = toRefs(userStore);

onMounted(async () => {
  try {
    await userStore.fetchMenus();
    console.log('菜单数据:', menus.value);
  } catch (error) {
    console.error('获取菜单数据失败:', error);
  }
});

const activeMenu = "1"; // 默认激活的菜单
</script>

<style scoped>

</style>